package com.ruoyi.web.controller.net;


import com.ruoyi.net.domain.ElementBgp;
import com.ruoyi.net.domain.ElementBgpEqual;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.IElementBgpEqualService;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.ElementBgpEqualBfd;
import com.ruoyi.net.service.IElementBgpEqualBfdService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元-BGP协议-BGP对等体配置-bfd属性Controller
 * 
 * @author net
 * @date 2024-11-17
 */
@Api(tags = "网元-BGP协议-BGP对等体配置-bfd属性")
@RestController
@RequestMapping("/element/bgp/equal/bfd")
public class ElementBgpEqualBfdController extends BaseController {
    @Autowired
    private IElementBgpEqualBfdService elementBgpEqualBfdService;

    @Autowired
    private IElementBgpEqualService elementBgpEqualService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询网元-BGP协议-BGP对等体配置-bfd属性列表
     */
    @ApiOperation(value = "查询网元-BGP协议-BGP对等体配置-bfd属性列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:bfd:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElementBgpEqualBfd elementBgpEqualBfd) {
        startPage();
        List<ElementBgpEqualBfd> list = elementBgpEqualBfdService.selectElementBgpEqualBfdList(elementBgpEqualBfd);
        return getDataTable(list);
    }

    /**
     * 导出网元-BGP协议-BGP对等体配置-bfd属性列表
     */
    @ApiOperation(value = "导出网元-BGP协议-BGP对等体配置-bfd属性列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:bfd:export')")
    @Log(title = "网元-BGP协议-BGP对等体配置-bfd属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElementBgpEqualBfd elementBgpEqualBfd) {
        List<ElementBgpEqualBfd> list = elementBgpEqualBfdService.selectElementBgpEqualBfdList(elementBgpEqualBfd);
        ExcelUtil<ElementBgpEqualBfd> util = new ExcelUtil<ElementBgpEqualBfd>(ElementBgpEqualBfd.class);
        util.exportExcel(response, list, "网元-BGP协议-BGP对等体配置-bfd属性数据");
    }

    /**
     * 获取网元-BGP协议-BGP对等体配置-bfd属性详细信息
     */
    @ApiOperation(value = "获取网元-BGP协议-BGP对等体配置-bfd属性详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:bfd:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(elementBgpEqualBfdService.selectElementBgpEqualBfdById(id));
    }

    /**
     * 新增网元-BGP协议-BGP对等体配置-bfd属性
     */
    @ApiOperation(value = "新增网元-BGP协议-BGP对等体配置-bfd属性", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:bfd:add')")
    @Log(title = "网元-BGP协议-BGP对等体配置-bfd属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElementBgpEqualBfd elementBgpEqualBfd) {
        sendXml(elementBgpEqualBfd);
        return toAjax(elementBgpEqualBfdService.insertElementBgpEqualBfd(elementBgpEqualBfd));
    }

    /**
     * 修改网元-BGP协议-BGP对等体配置-bfd属性
     */
    @ApiOperation(value = "修改网元-BGP协议-BGP对等体配置-bfd属性", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('system:bfd:edit')")
    @Log(title = "网元-BGP协议-BGP对等体配置-bfd属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElementBgpEqualBfd elementBgpEqualBfd) {
        sendXml(elementBgpEqualBfd);
        return toAjax(elementBgpEqualBfdService.updateElementBgpEqualBfd(elementBgpEqualBfd));
    }

    /**
     * 删除网元-BGP协议-BGP对等体配置-bfd属性
     */
    @ApiOperation(value = "删除网元-BGP协议-BGP对等体配置-bfd属性", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('system:bfd:remove')")
    @Log(title = "网元-BGP协议-BGP对等体配置-bfd属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(elementBgpEqualBfdService.deleteElementBgpEqualBfdByIds(ids));
    }

    private void sendXml(ElementBgpEqualBfd entity) {
        // TODO: 2024/12/5 待与设备联调
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getElementId());
            deviceManager.getDevice(netElement.getIp());

            ElementBgpEqual elementBgpEqual = elementBgpEqualService.selectElementBgpEqualById(entity.getBgpEqualId());

            StringBuilder xml = new StringBuilder();
            xml.append("<bgp xmlns=\"http://fiberhome.com/fhnw/yang/ipprotocol/routing/fos-bgp\">" +
                    "    <bgp-instances>" +
                    "      <bgp-instance>" +
                    "        <four-byte-as>" + elementBgpEqual.getAsId() + "</four-byte-as>" +
                    "        <no-vrf>" +
                    "          <neighbor>" +
                    // --- start BGP对等体配置/BFD属性 -----
                    "            <bfd-bgp-public>" +
                    "              <bfd-enable>" + entity.getBfdEnable() + "</bfd-enable>");
            if ("enable".equalsIgnoreCase(entity.getBfdEnable()) || "1".equals(entity.getBfdEnable())) {
                xml.append("              <bfd-multihop>" + entity.getBfd() + "</bfd-multihop>" +
                        "              <bfd-bgp-public-time-interval>" +
                        "                   <min-rx-interval>" + entity.getMinReceiveInterval() + "</min-rx-interval>" +
                        "                   <min-tx-interval>" + entity.getMinSendInterval() + "</min-tx-interval>" +
                        "                   <detect-multiplier>" + entity.getMonitorX() + "</detect-multiplier>" +
                        "              </bfd-bgp-public-time-interval>");
            }
            xml.append("            </bfd-bgp-public>" +
                    // --- end BGP对等体配置/BFD属性 -----
                    "          </neighbor>" +
                    "        </no-vrf>" +
                    "      </bgp-instance>" +
                    "    </bgp-instances>" +
                    "  </bgp>");
            System.out.println(xml.toString());
            deviceManager.editConfig(xml.toString(), "merge");
        }
    }
}